package 阿里笔试;
import 数据结构.数组.Array;

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            List<int[]> list = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                list.add(new int[]{a,b});
            }
            int k = sc.nextInt();
            int[] res = new int[k];
            for (int i = 0; i < k; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                res[i] = help(list,x,y);
            }
            for (int i = 0; i < k; i++) {
                System.out.println(res[i]);
            }
        }
    }


    static class ListNode{
        int val;
        ListNode next ;
        public ListNode(int val){
            this.val = val;
        }
    }

    public static int help(List<int[]> list,int x,int y){

        Map<Integer,ListNode> m = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            int a = list.get(i)[0];
            int b = list.get(i)[1];
            if(m.containsKey(a)){
                ListNode l2 = new ListNode(b);
                m.get(a).next = l2;
            }else{
                ListNode l1 = new ListNode(a);
                ListNode l2 = new ListNode(b);
                m.put(a,l1);
                m.put(b,l2);
                l1.next = l2;
            }
            int step = 0;
            ListNode l1 = m.get(x);
            ListNode l2 = m.get(y);
            while(l1!=null){
                l1 = l1.next;
                step++;
                if(l1==l2){
                    return step;
                }
            }
        }
        return -1;
    }
}
